<template>
  <div class="loadHomeWork">
    <div class="loadHomeWorkIneer">
      <div class="loadTop">船舶规范</div>
      <div class="loadAndSearch">
        <div class="loadExcel">
          <div
            class="button"
            type="success"
            @click="showPromise(certificateType)"
          >
            新增
          </div>
          <div class="buttonBack" @click="backIndexPage()">返回</div>
        </div>
      </div>

      <div class="table" :style="{ width: tableWidth }">
        <el-table
          empty-text="暂无数据"
          :header-cell-style="{
            background: '#FAFAFA',
            color: '#777676 !important',
            fontWeight: '800',
          }"
          :data="tableData"
          border
          stripe
          style="width: 100%; height: auto"
          @row-dblclick="handleRowDblclick"
          max-height="65vh"
          :row-style="{ height: 30 + 'px' }"
          :cell-style="{ padding: 0 + 'px' }"
        >
          <el-table-column
            label="序号"
            align="center"
            type="index"
            width="50"
          />
          <el-table-column
            :prop="shipName"
            label="船舶名称"
            align="center"
            min-width="100"
          >
            <template #default="scope">
              <div class="bigLight1">
                {{ shipName }}
                <div
                  class="bigLightSon"
                  style="width: 60px"
                  :data-text="scope.row"
                  @click="showPromise(scope.row.certificateType)"
                >
                  新增规范
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="name"
            label="规范名称"
            align="center"
            min-width="120"
          >
            <template #default="scope">
              <div
                class="bigLight"
                @click="
                  clickPDfName(
                    scope.row.file,
                    scope.row.id,
                    scope.row.certificateType,
                    scope.row.name,
                    scope.row.name + '-V' + scope.row.version + '.pdf'
                  )
                "
              >
                {{ scope.row.name }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            label="附件"
            min-width="110"
            prop="issueDate"
            align="center"
          >
            <template #default="scope">
              <div>{{ scope.row.name + "-" + "V" + scope.row.version }}</div>
            </template></el-table-column
          >
          <el-table-column
            label="开始实行时间"
            align="center"
            prop="executionDate"
            width="220"
          />
          <el-table-column
            prop="version"
            label="版本号"
            align="center"
            min-width="80"
          />
          <el-table-column
            prop="lastModifiedDate"
            label="最后更新时间"
            align="center"
            min-width="100"
          >
          </el-table-column>
        </el-table>
        <!-- 内容过多，会出现滚动条 -->
      </div>
      <div class="foot">
        <div class="currentPage">当前第{{ page }}/{{ totalPages }}页</div>
        <div
          class="lastPage"
          :style="{ background: color }"
          @click="clickLastPage()"
        >
          上一页
        </div>
        <div
          class="nextPage"
          :style="{ backgroundColor: color1 }"
          @click="clickNextPage()"
        >
          下一页
        </div>
        <div class="currentNumber">共{{ totalNumber }}条</div>
        <div>跳转至:</div>
        <input
          type="text"
          class="skipPage"
          style="width: 50px !important"
          v-model="shipPageNumer"
        />
        <div class="sureSkip" @click="skipPage">确定</div>
      </div>
    </div>
    <div class="dialog2" :style="{ display: uploadfile }">
      <div class="upContain">
        <input
          type="file"
          ref="clearFile"
          class="addfile"
          @change="getFile($event)"
          accept=".pdf"
        />
      </div>
      <div class="fileName">
        <div v-show="fileName">文件名:{{ fileName }}</div>
      </div>
      <div class="upFileSrue">
        <button class="buttonUpdata" @click="cancelUp()">取消</button>
        <button class="buttonUpdata buttonUpdata1" @click="submitAddFile()">
          确定
        </button>
      </div>
    </div>
    <div class="dialog" ref="dialog" :style="{ display: show }">
      <div class="loadHomeWorkIneer">
        <div class="changeTop">
          <div
            class="delete"
            @click="deleteOne(id)"
            v-show="addOrEdit == '编辑'"
          >
            删除
          </div>
          <div class="sure" @click="sureAdd()">确认</div>
          <div class="cancel" @click="close()">取消</div>
        </div>
        <div class="dialogText">{{ addOrEdit }}</div>
        <div class="manyInput">
          <div class="iuptInnerContain">
            <div class="inputInner">
              <div class="name">规范名称:</div>
              <div class="inputContainer">
                <input type="text" v-model="name" placeholder="规范名称" />
              </div>
            </div>
            <div class="inputInner">
              <div class="name">开始实行时间:</div>
              <div class="inputContainer">
                <el-config-provider :locale="locale">
                  <el-date-picker
                    v-model="executionDate"
                    type="date"
                    placeholder="开始实行时间"
                    format="YYYY/MM/DD"
                    value-format="YYYY/MM/DD"
                  />
                </el-config-provider>
              </div>
            </div>
            <div class="inputInner">
              <div class="name">版本号:</div>
              <div class="inputContainer">
                <input type="text" v-model="version" placeholder="版本号" />
              </div>
            </div>
            <!-- lastModifiedDate -->
            <div class="inputInner">
              <div class="name">最后更新时间:</div>
              <div class="inputContainer">
                <el-config-provider :locale="locale">
                  <el-date-picker
                    v-model="lastModifiedDate"
                    type="date"
                    placeholder="最后更新时间"
                    format="YYYY/MM/DD"
                    value-format="YYYY/MM/DD"
                  />
                </el-config-provider>
              </div>
            </div>
            <div
              class="addNewFile"
              style="margin-top: 100px"
              v-show="addOrEdit == '新增'"
            >
              <el-button type="success" @click="addFileNew()"
                >上传pdf附件</el-button
              >
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import EventBus from "@/mitt/eventBus.js";
import rename from "@/mitt/getPath.js";
import service from "@/api/api";
import zhCn from "element-plus/es/locale/lang/zh-cn";
import axios from "axios";

export default {
  name: "",
  data() {
    return {
      tableData: [],
      tableWidth: null,
      uploadfile: "none",
      fileName: "",
      totalPages: null,
      file: "",
      name: "",
      typeKey: "",
      havingNum: "",
      adventFlag: "",
      lastModifiedDate: "",
      id: null,
      color: "#EBEBEB",
      color1: "#EBEBEB",
      certificateType: "",
      totalNumber: "",
      filePath: "",
      page: 1,
      size: 20,
      shipName: "",
      show: "none",
      addOrEdit: "新增",
      locale: zhCn,
      executionDate: "",
      shipPageNumer: 1,
      lastModifiedDate: "",
      version: "",
      data: {
        cargoCapacity: "",
        cargoHoldType: "",
        certificateType: "",
        createTime: "",
        depthMoulded: "",
        executionDate: "",
        file: "",
        fillingStation: "",
        fullLoadDraft: "",
        gcu_capacity: "",
        hostPower: "",
        id: "",
        img: "",
        imo_number: "",
        lastModifiedDate: "",
        name: "",
        manning: "",
        marineMobilePhone: "",
        maxFillingRate: "",
        maxLoadingRate: "",
        moldedBreadth: "",
        name: "",
        sidePushPower: "",
        sts: "",
        totalLength: "",
        version: "",
        wail: "",
        mmsi_number: "",
        weitongNumber: "",
      },
    };
  },
  props: [],
  components: {},
  computed: {},
  filters: {},
  methods: {
    skipPage() {
      if (this.shipPageNumer <= this.totalPages && this.shipPageNumer > 0) {
        this.page = this.shipPageNumer;
        this.getRenderList();
      } else {
        this.$message({
          type: "warning",
          message: "超出页面数量范围",
        });
      }
    },
    clickLastPage() {
      if (this.page > 1) {
        this.page--;
        if (this.page == 1) {
          this.color = "#EBEBEB";
        } else {
          this.color = "#268FDE";
        }
        if (this.page < this.totalPages) {
          this.color1 = "#268FDE";
        }
        this.getRenderList();
      } else {
        this.color = "#EBEBEB";
      }
    },
    clickNextPage() {
      if (this.page < this.totalPages) {
        this.page++;
        this.color = "#268FDE";
        if (this.page == this.totalPages) {
          this.color1 = "#EBEBEB";
        } else {
          this.color1 = "#268FDE";
        }
      }
      this.getRenderList();
    },
    addFileNew() {
      this.uploadfile = "block";
    },
    clearChan() {
      this.data = {
        file: "",
        name: "",
        executionDate: "",
        version: "",
        lastModifiedDate: "",
      };
      this.filePath = "";
      this.fileName = "";
      this.executionDate = "";
      this.name = "";
      this.version = "";
      this.lastModifiedDate = "";
    },
    clickEdit(a) {
      this.addOrEdit = "编辑";
      this.tableData.forEach((item) => {
        if (item.id == a) {
          this.data = item;
          this.name = item.name;
          this.executionDate = item.executionDate;
          this.filePath = item.file;
          this.id = item.id;
          this.version = item.version;
          this.lastModifiedDate = item.lastModifiedDate;
        }
      });
    },
    handleRowDblclick(row) {
      this.show = "block";
      this.clickEdit(row.id);
    },
    deleteOne(a) {
      // let that = this;
      this.$alert("确定删除这条信息？", {
        confirmButtonText: "确定",
        callback: (action) => {
          if (action == "confirm") {
            service({
              method: "delete",
              url: `/ship-standard/${a}`,
            }).then((r) => {
              if (r.status == "SUCCESS") {
                this.$message({
                  type: "success",
                  message: "删除成功",
                });
                this.getRenderList();
                this.clearChan();
                this.show = "none";
              }
            });
          } else {
            this.$message({
              type: "info",
              message: "取消删除",
            });
          }
        },
      });
    },
    backIndexPage() {
      this.$router.push("/app.shipSpecificationAdd");
    },
    getFile(event) {
      this.file = event.target.files[0];
      this.fileName = this.file.name;
      document.getElementsByClassName("addfile")[0].value = "";
    },
    cancelUp() {
      this.file = "";
      this.fileName = "";
      this.uploadfile = "none";
      document.getElementsByClassName("addfile")[0].value = "";
    },
    submitAddFile() {
      let data = {
        file: this.file,
        type: 6,
      };
      service({
        method: "post",
        url: "/system/upload-file",
        headers: {
          "Content-Type": "multipart/form-data",
        },
        data: data,
      })
        .then((res) => {
          this.filePath = res.data;
          this.uploadfile = "none";
          this.$message({
            message: "文件选择成功！",
            type: "success",
          });
        })
        .catch((err) => {
          this.uploadfile = "none";
          this.$message({
            message: err,
            type: "fail",
          });
        });
    },

    close() {
      this.show = "none";
      this.clearChan();
    },
    sureAdd() {
      this.data.certificateType = this.certificateType;
      this.data.file = this.filePath;
      this.data.name = this.name;
      this.data.executionDate = this.executionDate;
      this.data.id = this.id;
      this.data.version = this.version;
      this.data.lastModifiedDate = this.lastModifiedDate;

      if (!this.version || !this.name) {
        return this.$message({
          message: "规范名称和版本号不可为空",
          type: "warning",
        });
      }
      // return;
      if (this.addOrEdit == "新增") {
        if (!this.filePath) {
          return this.$message({
            message: "规范文件未选择",
            type: "warning",
          });
        }
        service({
          method: "post",
          data: this.data,
          url: "/ship-standard",
        })
          .then((r) => {
            if (r.status == "SUCCESS") {
              this.$message({
                type: "success",
                message: "新增成功",
              });
            }
            this.getRenderList();
            this.show = "none";
            this.clearChan();
          })
          .catch((err) => {
            this.$message({
              type: "info",
              message: "出错了！",
            });
          });
      } else if (this.addOrEdit == "编辑") {
        service({
          method: "PUT",
          data: this.data,
          url: "/ship-standard",
        })
          .then((r) => {
            if (r.status == "SUCCESS") {
              this.$message({
                type: "success",
                message: "修改成功",
              });
            }
            this.getRenderList();
            this.show = "none";
            this.clearChan();
          })
          .catch((err) => {
            this.$message({
              type: "info",
              message: "出错了！",
            });
          });
      }
    },
    showPromise(certificateType) {
      this.addOrEdit = "新增";
      if (certificateType) {
        this.certificateType = certificateType;
      } else {
        return this.$message({
          message: "关键数据缺失,无法上传文件",
          type: "danger",
        });
      }
      this.show = "block";
    },
    clickPDfName(file, id, type, name, scope) {
      // return;
      if (!file) {
        return this.$message({
          message: "不存在文件！",
          type: "warning",
        });
      }
      this.$router.push({
        path: "/app.shipSpecificationSeeThird",
        query: {
          id: id,
          name: name,
          certificateType: type,
          file: file,
          shipName: this.shipName,
          loadName: scope,
        },
      });
    },
    handleMouseOver(a) {
      try {
        a.target.children[0].style.display = "block";
      } catch (error) {}
    },
    handleMouseLeave(a) {
      try {
        a.target.children[0].style.display = "none";
      } catch (error) {}
    },

    // 导出文件
    sendMessage() {
      let res = rename();
      EventBus.emit("custom-event", res);
    },

    //单个角色

    // 获取列表数据
    async getRenderList() {
      let data = {
        page: this.page,
        size: this.size,
        certificateType: this.certificateType,
      };
      await service({
        methods: "get",
        url: "/ship-standard/find-one-page",
        params: data,
      })
        .then((e) => {
          this.tableData = e.data.content;
          this.totalPages = e.data.totalPages;
          this.currentNum = e.data.numberOfElements;
          this.totalNumber = e.data.totalElements;
          if (this.totalPages == 0) {
            this.totalPages = 1;
          }
          if (this.page < this.totalPages) {
            this.color1 = "#268FDE";
          } else {
            this.color1 = "#EBEBEB";
          }
          if (this.page == 1) {
            this.color = "#EBEBEB";
          } else {
            this.color = "#268FDE";
          }
        })
        .catch((err) => {});
    },

    handSize() {
      if (window.innerWidth > 630) {
        if (this.$store.state.width == 200) {
          this.tableWidth = window.innerWidth - 220 + "px";
        } else if (this.$store.state.width == 90) {
          this.tableWidth = window.innerWidth - 100 + "px";
        }
      } else {
        this.tableWidth = window.innerWidth - 20 + "px";
      }
    },
  },
  watch: {
    "$store.state.width"(newVal, oldVal) {
      if (window.innerWidth > 630) {
        if (newVal == 90) {
          this.tableWidth = (window.innerWidth - 60) * 0.985 - 20 + "px";
        } else if (newVal == 200) {
          this.tableWidth = (window.innerWidth - 170) * 0.985 - 20 + "px";
        }
      }
    },
  },

  created() {
    this.sendMessage();
    this.certificateType = this.$route.query.id;
    this.shipName = this.$route.query.name;
  },
  beforeMount() {
    if (window.innerWidth > 630) {
      if (this.$store.state.width == 200) {
        this.tableWidth = (window.innerWidth - 170) * 0.985 - 20 + "px";
      } else if (this.$store.state.width == 90) {
        this.tableWidth = (window.innerWidth - 60) * 0.985 - 20 + "px";
      }
    } else {
      this.tableWidth = window.innerWidth - 30 + "px";
    }
  },
  mounted() {
    let that = this;
    this.getRenderList();
    window.addEventListener("resize", this.handSize);
  },
  beforeUnmount() {
    window.removeEventListener("resize", this.handSize);
  },
};
</script>
<style lang="less" scoped>
.dialog,
.dialog1 {
  width: 100%;
  height: 100%;
  background: #dfe6e8;
  position: absolute;
  top: 0;
  left: 0;
  display: none;
  z-index: 100;
}
.dialog2 {
  z-index: 1000;
}
.loadHomeWork {
  overflow: hidden;
  width: 100%;
  height: 100%;
  position: relative;
  .loadHomeWorkIneer {
    width: 98.5%;
    height: 91vh;
    margin: auto;
    margin-top: 1vh;
    background: white;
    .loadTop {
      width: 50%;
      line-height: 4vh;
      color: #777676;
      // text-indent: 10px;
      margin-left: 10px;
      font-size: 20px;
      font-weight: 800;
    }
    .loadSecond {
      width: 280px;

      color: #777676;
      text-align: center;
      line-height: 33px;
      color: #777676;
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-left: 10px;
      font-size: 15px;
      .topName {
        width: 90px;
        line-height: 33px;
        background: #67c23a;
        border-radius: 5px;
        a {
          display: block;
          width: 90px;
          line-height: 33px;
          text-decoration: none;
          color: white;
        }
      }
      .topName:nth-child(3) {
        background: #242121;
      }
    }
    // .loadExcel {
    //   width: 200px;
    //   margin-top: 10px;
    //   margin-left: 10px;

    //   height: 20px;
    //   display: flex;
    //   align-items: center;
    //   justify-content: space-between;
    //   .button {
    //     width: 60px;
    //     line-height: 20px;
    //     font-size: 18px;
    //     text-align: center;
    //   }
    // }
  }
  // background: blue;
}
.table {
  width: 98.5%;
  margin: auto;
  // margin-top: 2vh;
  height: 65vh;
  // background: red;
}
.manyInput {
  width: 780px;
  height: 80vh;
  overflow: auto;
  // background: red;
  margin: auto;

  // overflow: scroll;
  .iuptInnerContain {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    .inputInner {
      width: 375px;
      min-height: 35px;
      // background: blue;
      margin-right: 10px;
      margin-top: 10px;
      display: flex;
      align-items: center;
      // justify-content: space-between;
      color: #365488;
      .name {
        width: 150px;
        height: 100%;
        line-height: 4vh;
        text-align: right;
        font-size: 15px;
      }
      // .name1 {
      //   width: 210px;
      //   margin-left: 10px;
      //   height: 3vh;
      //   line-height: 3vh;
      //   text-indent: 1em;
      //   outline: none;
      //   font-size: 15px;
      //   border: none;
      //   background: #ebebeb;
      //   color: #365488;
      // }
      .inputContainer {
        width: 210px;
        margin-left: 5px;
      }
      .el-input__inner {
        background: white !important;
      }
      input {
        width: 220px;
        height: 34px;
        margin-left: 5px;
        border-radius: 5px;
        text-indent: 1em;
        outline: none;
        border: none;
        background: white !important;
        color: #365488;
        border: 1px solid #dcdfe6;
      }
      input::-webkit-input-placeholder {
        color: #a8abb2;
      }
      input[type="date"] {
        // padding: 5px;
        color: #365488;
        text-align: left;
        // border: 1px solid #365488;
        border-radius: 4px;
        // background-color: #fff;
        box-sizing: border-box; /* 确保内边距和边框不会影响元素的总宽度 */
      }
      // input[type="number"] {
      //   appearance: textfield;
      //   -moz-appearance: textfield;
      // }
      // 　　input[type="number"]::-webkit-inner-spin-button,
      // 　　input[type="number"]::-webkit-outer-spin-button {
      //   -webkit-appearance: none;
      //   margin: 0;
      // }
    }
    .el-date-picker .el-picker-panel {
      background-color: white !important; /* 你想要的背景颜色 */
    }
  }
}

/* 必须是input为 checkbox class 添加 switch 才能实现以下效果 */

@media (min-width: 0px) and (max-width: 960px) {
  .manyInput {
    width: 375px !important;
    display: block !important;
    .inputInner {
      margin: auto !important;
      // margin-top: 10px !important;
    }
  }
  .closeAndSrue {
    width: 110px;
  }
}
</style>
